package com.heng.LockSupportDemo;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
//lock锁是不能被注销的，await和signal是必须放在lock和unlock之间的，才能正常使用！
//其次是顺序是await在前，signal在后，否则会陷入死循环！等待唤醒的会一直阻塞！
public class LockSupportDemo4 {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(() -> {
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t---come in");
                condition.await();
                System.out.println(Thread.currentThread().getName()+"\t---被唤醒");
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                lock.unlock();
            }
        },"t1").start();

        try {TimeUnit.MILLISECONDS.sleep(10);}catch (InterruptedException e) {e.printStackTrace();}

        new Thread(() -> {
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName()+"\t---发出通知");
            }finally {
                lock.unlock();
            }
        },"t2").start();
    }
}